﻿using Koala.Pro.CodeGenerator.UI.Common;
using Koala.Pro.CodeGenerator.UI.DatabaseModel;

namespace Koala.Pro.CodeGenerator.UI
{
    public partial class FrmConfigure : Form
    {
        private MainSetting setting = new MainSetting();

        private DatabaseSetting[] dataSettings;

        private string assemblyType = string.Empty;

        private string databaseType = string.Empty;

        public FrmConfigure()
        {
            InitializeComponent();
            InitTreeView();
        }

        private void InitTreeView()
        {
            treeView1.Nodes.Clear();
            treeView1.BeginUpdate();
            Cursor.Current = Cursors.WaitCursor;
            TreeNode treeNode = new TreeNode("SqlServer", 3, 3);
            treeNode.Tag = "Koala.Pro.CodeGenerator.UI.DatabaseModel.Data.SqlServer";

            TreeNode treeNode2 = new TreeNode("Oracle", 2, 2);
            treeNode2.Tag = "Koala.Pro.CodeGenerator.UI.DatabaseModel.Data.Oracle";
 
            TreeNode treeNode3 = new TreeNode("Mysql", 1, 1);
            treeNode3.Tag = "Koala.Pro.CodeGenerator.UI.DatabaseModel.Data.MySql";

            TreeNode treeNode4 = new TreeNode("Sqlite", 4, 4);
            treeNode4.Tag = "Koala.Pro.CodeGenerator.UI.DatabaseModel.Data.Sqlite";

            dataSettings = DatabaseConfig.ReadSettings();
            TreeNode treeNode6 = null;
            DatabaseSetting[] array = dataSettings;
            foreach (DatabaseSetting databaseSetting in array)
            {
                treeNode6 = new TreeNode(databaseSetting.DatabaseType, 0, 0);
                treeNode6.ImageIndex = 0;
                treeNode6.Tag = databaseSetting.AssemblyType;
                if (databaseSetting.AssemblyType.Equals("Koala.Pro.CodeGenerator.UI.DatabaseModel.Data.SqlServer", StringComparison.OrdinalIgnoreCase))
                {
                    treeNode.Nodes.Add(treeNode6);
                }
                else if (databaseSetting.AssemblyType.Equals("Koala.Pro.CodeGenerator.UI.DatabaseModel.Data.Oracle", StringComparison.OrdinalIgnoreCase))
                {
                    treeNode2.Nodes.Add(treeNode6);
                }
                else if (databaseSetting.AssemblyType.Equals("Koala.Pro.CodeGenerator.UI.DatabaseModel.Data.MySql", StringComparison.OrdinalIgnoreCase))
                {
                    treeNode3.Nodes.Add(treeNode6);
                }
                else if (databaseSetting.AssemblyType.Equals("Koala.Pro.CodeGenerator.UI.DatabaseModel.Data.Sqlite", StringComparison.OrdinalIgnoreCase))
                {
                    treeNode4.Nodes.Add(treeNode6);
                }
            }
            treeView1.Nodes.AddRange([treeNode, treeNode2, treeNode3, treeNode4]);
            treeView1.ExpandAll();
            Cursor.Current = Cursors.Default;
            treeView1.EndUpdate();
        }

        private void LoadProjectData()
        {
            try
            {
                setting.Load();
                txtOutputDir.Text = setting.OutputDir;
                txtRootNameSpace.Text = setting.RootNameSpace;
                txtNoPrefix.Text = setting.NoPrefixOfTableName;
            }
            catch (Exception ex)
            {
                MessageUtil.ShowError("加载项目配置信息失败：" + ex.Message);
            }
        }

        private void ConfigureForm_Load(object sender, EventArgs e)
        {
            LoadProjectData();
        }

        private void ctx_Delete_Click(object sender, EventArgs e)
        {

        }

        private void ctx_Modify_Click(object sender, EventArgs e)
        {
            DialogResult dialogResult = DialogResult.None;
            switch (assemblyType)
            {
                case "Koala.Pro.CodeGenerator.UI.DatabaseModel.Data.SqlServer":
                case "Koala.Pro.CodeGenerator.UI.DatabaseModel.Data.Oracle":
                case "Koala.Pro.CodeGenerator.UI.DatabaseModel.Data.MySql":
                    {
                        FrmRegisterDb registerDatabase = new FrmRegisterDb();
                        registerDatabase.cmbDbType.Text = assemblyType;
                        registerDatabase.DatabaseType = databaseType;
                        registerDatabase.StartPosition = FormStartPosition.CenterScreen;
                        dialogResult = registerDatabase.ShowDialog();
                        break;
                    }
                default:
                    {
                        //RegisterCustomedDB registerCustomedDB = new RegisterCustomedDB();
                        //registerCustomedDB.StartPosition = FormStartPosition.CenterScreen;
                        //registerCustomedDB.DatabaseType = databaseType;
                        //dialogResult = registerCustomedDB.ShowDialog();
                        break;
                    }
            }
            if (DialogResult.OK == dialogResult)
            {
                InitTreeView();
            }
        }

        private void ctx_RegisterNew_Click(object sender, EventArgs e)
        {

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            SaveProjectData();
            this.DialogResult = DialogResult.OK;
        }

        private void SaveProjectData()
        {
            setting.OutputDir = txtOutputDir.Text;
            setting.RootNameSpace = txtRootNameSpace.Text;
            setting.NoPrefixOfTableName = txtNoPrefix.Text;
            try
            {
                setting.Save();
            }
            catch (Exception ex)
            {
                MessageUtil.ShowError("保存项目配置文件失败：" + ex.Message);
            }
        }

        private void btnBrowsePath_Click(object sender, EventArgs e)
        {

        }

        private void treeView1_MouseDown(object sender, MouseEventArgs e)
        {
            TreeNode nodeAt = treeView1.GetNodeAt(e.X, e.Y);
            if (nodeAt == null)
            {
                return;
            }
            treeView1.SelectedNode = nodeAt;
            databaseType = nodeAt.Text;
            assemblyType = nodeAt.Tag.ToString();
            if (e.Button == MouseButtons.Right)
            {
                treeView1.ContextMenuStrip = contextMenuStrip1;
                byte nodeLevel = Program.global.GetNodeLevel(nodeAt);
                if (nodeLevel == 2)
                {
                    ctx_Delete.Enabled = true;
                    ctx_Modify.Enabled = true;
                }
                else
                {
                    ctx_Delete.Enabled = false;
                    ctx_Modify.Enabled = false;
                }
            }
        }

        private void treeView1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            TreeNode nodeAt = treeView1.GetNodeAt(e.X, e.Y);
            byte nodeLevel = Program.global.GetNodeLevel(nodeAt);
            if (e.Button == MouseButtons.Left && nodeLevel == 2)
            {
                ctx_Modify_Click(null, null);
            }
        }
    }
}
